<template>
  <div class="login_container">
    <div class="login_bg"></div>
    <div class="login_box">
      <h1>智能驾驶行为分析及预警系统</h1>
      <h2>请输入账号、密码登入系统</h2>
      <!-- 登录表单区域 -->
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="iconfont icon-3702mima" type="password" show-password></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
          <el-button type="success" @click="registerLoginForm = true">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--   注册表单区域   -->
    <div>
      <el-dialog title="用户注册" width="40%"  :visible.sync="registerLoginForm" @close="addDialogClosed">
<!--    内容主体区域    -->
        <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="addForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="addForm.password"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="addForm.mobile"></el-input>
          </el-form-item>
          <el-form-item label="验证码" prop="verification">
            <el-input v-model="addForm.verification"></el-input>
          </el-form-item>
          <el-form-item label="邮箱号" prop="email">
            <el-input v-model="addForm.email"></el-input>
          </el-form-item>
        </el-form>
<!--    底部区域    -->
        <span slot="footer">
    <el-button @click="registerLoginForm = false">取 消</el-button>
    <el-button type="primary" @click="addUser">确 定</el-button>
  </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    // 验证邮箱的规则
    var checkEmail = (rule, value, cb) => {
      // 验证邮箱的正则表达式
      const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/

      if (regEmail.test(value)) {
        // 合法的邮箱
        return cb()
      }

      cb(new Error('请输入合法的邮箱'))
    }

    // 验证手机号的规则
    var checkMobile = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/

      if (regMobile.test(value)) {
        return cb()
      }

      cb(new Error('请输入合法的手机号'))
    }
    //验证码的规则
    return {
      //控制用户注册对话框的显示与隐藏
      registerLoginForm:false,
      //用户注册的表单数据
      addForm:{
        username:'',
        password:'',
        mobile:'',
        verification:'',
        email:'',

      },
      //用户注册的验证规则对象
      addFormRules:{
        username:[
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {min: 3, max: 10, message: '用户名的长度在3~10个字符之间', trigger: 'blur'}
        ],
        password:[
          { required: true, message: '请输入密码', trigger: 'blur' },
          {min: 6, max: 15, message: '用户名的长度在6~15个字符之间', trigger: 'blur'}
        ],
        mobile:[
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ],
        verification: [

        ],
        email:[
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' }
        ]
      },
      // 这是登录表单的数据绑定对象
      loginForm: {
        username: '',//admin
        password: '',//123456
      },
      // 这是表单的验证规则对象
      loginFormRules: {
        // 验证用户名是否合法
        username: [
          {required: true, message: '请输入登录账号', trigger: 'blur'},
          {min: 2, max: 10, message: '账号长度在 3 到 10 个字符', trigger: 'blur'}
        ],
        // 验证密码是否合法
        password: [
          {required: true, message: '请输入登录密码', trigger: 'blur'},
          {min: 6, max: 15, message: '密码长度在 6 到 15 个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    // 点击重置按钮，重置登录表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    // login() {
    //   this.$refs.loginFormRef.validate(async valid => {
    //     if (!valid) return
    //     const { data: res } = await this.$http.post('login', this.loginForm)
    //     if (res.meta.status !== 200) return this.$message.error('登录失败！')
    //     this.$message.success('登录成功')
    //     // 1. 将登录成功之后的 token，保存到客户端的 sessionStorage 中
    //     //   1.1 项目中出了登录之外的其他API接口，必须在登录之后才能访问
    //     //   1.2 token 只应在当前网站打开期间生效，所以将 token 保存在 sessionStorage 中
    //     window.sessionStorage.setItem('token', res.data.token)
    //     // 2. 通过编程式导航跳转到后台主页，路由地址是 /home
    //     this.$router.push('/home')
    //   })
    // }
    // 监听添加用户对话框的关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields()
    },
    // 点击按钮，注册新用户
    addUser() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        // 可以发起注册用户的网络请求
        const { data: res } = await this.$http.post('username', this.addForm)

        if (res.meta.status !== 201) {
          this.$message.error('添加用户失败！')
        }

        this.$message.success('添加用户成功！')
        // 隐藏注册用户的对话框
        this.registerLoginForm = false
      })
    },
    login() {
      if (this.$data.loginForm.username === 'xm' && this.$data.loginForm.password === '123456') {
        this.$router.push('/home')
        return this.$message.success('登录成功!')
      }
      this.$message.error('登录失败！')
    },
    registerLoginForm() {

    }
  }
}
</script>

<style Lang="less" scoped>
.login_box {
  width: 450px;
  height: 300px;
  z-index: 1;
  background: rgba(0.3, 0.3, 0.3, 0.3);
  border-radius: 25px;
  position: absolute;
  left: 49%;
  top: 47%;
  transform: translate(-50%, -50%);
}

.login_bg {
  background: url("../assets/background.png") no-repeat center;
  height: 100%;
  width: 100%;
  background-size: cover;
  position: fixed;
  z-index: -1;
}

h1, h2 {
  text-align: center;
  color: #ffffff;
  margin-bottom: -15px;
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
